if(!dojo._hasResource["opm.widgets.component.ClassDef"]){
dojo._hasResource["opm.widgets.component.ClassDef"] = true;
dojo.provide("opm.widgets.component.ClassDef");
dojo.require("dijit._Widget");
dojo.require("opm.widgets.Ajax");
dojo.require("opm.widgets.GenericForm");
dojo.require("opm.widgets.GenericPage");
dojo.require("opm.widgets.GenericFormDialog");
dojo.require("opm.widgets.GenericStack");
dojo.declare("opm.widgets.component.ClassDef",dijit._Widget,
{
   contentId:"",
   defaultURL:"",
   _GENERIC_PAGE:"ClassDef.genericPage",
   _GENERIC_ADD_DESCRIPTION_FORM:"ClassDef.addForm.Description",
   _GENERIC_ASSIGN_PROPERTY_FORM:"ClassDef.addForm.Property",
   _GENERIC_ASSIGN_SECURITY_FORM:"ClassDef.Security.add",
   _GENERIC_REVIEW_FORM:"ClassDef.addForm.Review",
   _GENERIC_EDIT_FORM:"ClassDef.editForm",
   stackObject:null,
   constructor:function(args){
      this.setAtts(args);
   },
   setAtts:function(args){
      this.contentId = args.contentId;
   },
   displayClassDefMenu:function(){
      var args = {ajaxUrl: this.defaultURL, callback: this._initClassDef};
      OPM.ajaxWidget(args, this.contentId).callXhrGet();
   },

   showAddForm:function(ajaxUrl){
      var args = {ajaxUrl: ajaxUrl, callback: this._initAddPageClassDef};
      OPM.ajaxWidget(args, this.contentId).callXhrGet();
   },

   showDescriptionForm:function(ajaxUrl){
      var dataform = {
         ajaxCallUrl: ajaxUrl, 
         ajaxCallType:'callXhrGetJson',
         contentId: 'contentStack',
         widgetId: this._GENERIC_ADD_DESCRIPTION_FORM
      };
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
          OPM.classDefWidget().displayClassDefMenu();
      });
      genericForm.connectEvent("btnAddClick", function(data){
         genericForm.callSaveForm({handleSuccess:function(){
            stackObject.gotoNextItem();
         }});
      });
      genericForm.createForm();
   },

   showSecurities:function(ajaxUrl){
      var args = {ajaxUrl: ajaxUrl, callback: this._initSecurities};
      OPM.ajaxWidget(args,'contentStack').callXhrGet();
   },

   showPropertiesForm:function(ajaxUrl){
      var dataform = {
         ajaxCallUrl: ajaxUrl, 
         ajaxCallType:'callXhrGetJson',
         contentId: 'contentStack',
         widgetId: this._GENERIC_ASSIGN_PROPERTY_FORM
      };
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
          OPM.classDefWidget().displayClassDefMenu();
      });
      genericForm.connectEvent("btnAddClick", function(data){
        genericForm.callSaveProperties(["JSONPropertyIds"], {handleSuccess:function(){
           stackObject.gotoNextItem();
        }});
      });
      genericForm.createForm();
   },

  showReviewForm:function(ajaxUrl){
      var dataform = {
         ajaxCallUrl: ajaxUrl, 
         ajaxCallType:'callXhrGetJson',
         contentId: 'contentStack',
         widgetId: this._GENERIC_REVIEW_FORM
      };
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
          OPM.classDefWidget().displayClassDefMenu();
      });
      genericForm.connectEvent("btnAddClick", function(data){
         genericForm.callSaveForm({handleSuccess:function(){
            refreshCurrentNodeItemTree();
            OPM.classDefWidget().displayClassDefMenu();
         }});
      });
      genericForm.createForm();
   },

   commonForm:function(dataform){
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
          OPM.classDefWidget().displayClassDefMenu();
      });
      genericForm.connectEvent("btnAddClick", function(data){
         genericForm.callSaveForm({
            handleSuccess:function(){
                stackObject.gotoNextItem();
            }, 
            handleError: function (result){
                alert("Can not create this Class");
            }
         });
      });
      return genericForm;
   },

   _initClassDef:function(){
      dojo.query('#hrefViewCreateClassDef').onclick(function(){
         OPM.classDefWidget().showAddForm(dojo.byId('viewCreateClassDef').value);
      });

      dojo.query('#hrefViewClassDefEditForm').onclick(function(){
         OPM.classDefWidget().showAddForm(dojo.byId('viewEditClassDef').value);
      });

     var defaultURLValue = this.defaultURL;
      dojo.query('table#classInst tbody tr').connect('ondblclick', function(evt){
         var editUrl = dojo.query('input[type="hidden"]', this)[0].value;
         OPM.classInstWidget({defaultURL:defaultURLValue},'center').showEditForm(editUrl);
      });

      dojo.query('#hrefViewCreateClassInst').onclick(function(){
         OPM.classInstWidget({defaultURL:defaultURLValue},'center').showCreateForm(dojo.byId('viewCreateClassInst').value);
      });
   },

   _initAddPageClassDef:function(){
      var descriptions = dojo.byId('titleDescription').value.split("||");
      var properties = dojo.byId('titleProperties').value.split("||");
      var securities = dojo.byId('titleSecurities').value.split("||");
      var reviews = dojo.byId('titleReview').value.split("||");
      var hiddenIsEditClassDefPage = dojo.byId('isEditClassDefPage');
      var isEditClassDefPage = (hiddenIsEditClassDefPage != null && hiddenIsEditClassDefPage.value == "true")?false:true;
      stackObject = new opm.widgets.GenericStack({
        data:[{title:descriptions[0], href:descriptions[1], type:'description'},
              {title:properties[0], href:properties[1], type:'properties'},
              {title:securities[0], href:securities[1], type:'securities'},
              {title:reviews[0], href:reviews[1], type:'review'}],
        contentId:'stackMenuDiv', hasDisabledStackInactiveYet:isEditClassDefPage});
      stackObject.connectEvent("selectedItem",function (item){
         if (item.type == 'description' ){
            OPM.classDefWidget().showDescriptionForm(item.href);
         }else if (item.type == 'properties'){
            OPM.classDefWidget().showPropertiesForm(item.href);
         }else if (item.type == 'securities'){
            OPM.classDefWidget().showSecurities(item.href);
         }else if (item.type == 'review'){
            OPM.classDefWidget().showReviewForm(item.href);
         }
      });
      stackObject.generic();
   },

   _initSecurities:function(){
      dojo.query('table#sec tbody tr').connect('ondblclick', function(evt){
         var editUrl = dojo.query('input[type="hidden"]', this)[0].value;
         var listUrl = dojo.byId('hrefListSecurities').value;
         OPM.securityWidget({},'contentStack').showEditForm(editUrl,{cancel:function(){OPM.classDefWidget().showSecurities(listUrl);},
            succ:function(){OPM.classDefWidget().showSecurities(listUrl);}});
      });
      dojo.query('#hrefCancel').onclick(function(){
         OPM.classDefWidget().displayClassDefMenu();
      });
      dojo.query('#hrefNext').onclick(function(){
          stackObject.gotoNextItem();
      });
      dojo.query('#hrefAssignPermision').onclick(function(){
         var searchUrl = dojo.query('input[type="hidden"]', this)[0].value;
         var listUrl = dojo.byId('hrefListSecurities').value;
         OPM.securityWidget({},'contentStack').showSecuritiesForm(searchUrl,function(){
            OPM.classDefWidget().showSecurities(listUrl);
         });
      });
   }
});}
